'use strict'
let underscore = require('underscore')
let schedule = require('node-schedule');
let templateConfig = require('../../config/templateConfig')
let sceneMgr = require('../core/sceneMgr')
let playerSessionMgr = require('../core/playerSessionMgr')
let FieldBossComponent = require('./fieldBossComponent')
let pushMsg = require('../nodeapp/pushMsg')
let totalConfig = templateConfig.totalConfig
//野外boss
class GuardComponent {
  constructor() {
    this.fieldBoss = {
      260002: 0,
    }
    this.factor = 0
    this.fieldBossId = {
      260002: totalConfig[260002].guardFieldBoss
    }

  }

  //初始化地图上的boss
  createFieldBoss(sceneId) {
    Log('创建boss')
    let monsterId = this.fieldBossId[sceneId]
    let fieldBossConfig = totalConfig[monsterId]
    fieldBossConfig['sceneId'] = sceneId
    let fieldBoss = new FieldBossComponent(fieldBossConfig)
    this.fieldBoss[sceneId] = fieldBoss
    this.noticeFieldBoss(sceneId, fieldBoss.name)
  }

  getFieldBossInfo(sceneId, pId) {
    if (!this.fieldBoss[sceneId]) {
      return {
        fieldBossInfo: 0,
        myDamage: 0
      }
    } else {
      let fieldBoss = this.fieldBoss[sceneId].fieldBoss
      let fightAttri = fieldBoss.fightAttri
      let info = this.fieldBoss[sceneId].playersMap.get(pId)
      let myDamage = 0
      if(info){
        myDamage = info.damage
      }else{
        myDamage = 0
      }
      return {
        fieldBossInfo: Math.ceil(fightAttri.hp / fightAttri.maxHp * 10000),
        myDamage: myDamage
      }
    }
  }

  noticeFieldBoss(sceneId, fieldBossName) {
      //推送野外boss生成消息
    pushMsg.noticeFieldBoss(sceneId, {
      name: fieldBossName,
    })
  }

  attackFieldBoss(player, sceneId) {
    let self = this
    let fieldBoss = this.fieldBoss[sceneId]
    if (!fieldBoss) {
      return -1
    }
    let fightMsg = fieldBoss.attackFieldBoss(player)
    let result = fightMsg.result
    if (result === 1) {
      let nowTime = new Date().getTime()
      if(nowTime - this.fieldBoss[sceneId].createTime < 900000){//15分钟
        this.factor += 1
      }else if(this.factor > 1 ){
        this.factor -= 1
      }
      this.fieldBoss[sceneId] = null
      //boss死了,隔40分钟复活
      setTimeout(function(){
        self.createFieldBoss(260002, self.factor)
      }, 2400000)
    }
    return fightMsg
  }
}

let guardComponent = new GuardComponent()
module.exports = guardComponent

setTimeout(function(){
  guardComponent.createFieldBoss(260002)
},5000)


/* let rule = new schedule.RecurrenceRule();
rule.minute = [0, 30];
let j = schedule.scheduleJob(rule, function() {
   if (wbFam.state === 0) {
	      Log('wbFam.state  ' + wbFam.state )
	      wbFam.state = 1
	      wbFam.boss = null
	      wbFam.createMonsters()
	  }
});
*/
